﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Framework_Tool
{

    public partial class StudentAdd : Page
    {
        public int courseID = 0;
        public StudentAdd()
        {
            InitializeComponent();
        }

        private void mainContent_Loaded_1(object sender, RoutedEventArgs e)
        {
            string id = Database.NextStudentID();
            stdtIDAddTxt.Text = id;

            Database.FindStdtCourseTitle_Add(this);

            viewStudentBtn.FontWeight = FontWeights.Normal;
            addStudentBtn.FontWeight = FontWeights.ExtraBold;
            updateStudentBtn.FontWeight = FontWeights.Normal;
            deleteStudentBtn.FontWeight = FontWeights.Normal;
            searchStudentBtn.FontWeight = FontWeights.Normal;
        }

        private void stdtPicBrowse_Click(object sender, RoutedEventArgs e)
        {   
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Create OpenFileDialog box
            
            dlg.DefaultExt = ".jpg"; // Default file extension

            Nullable<bool> result = dlg.ShowDialog(); // Calls ShowDialog method
            
            if (result == true) // display selected file name in a TextBox
            {
                string filename = dlg.FileName;
                stdtPicTxt.Text = filename;
            }
        }

        private void viewStudentBtn_Click_1(object sender, RoutedEventArgs e)
        {
            StudentView stdtViewWindow = new StudentView();
            stdtViewWindow.Content = null;
            mainContent.Children.Add(stdtViewWindow.mainContent);
        }

        private void updateStudentBtn_Click_1(object sender, RoutedEventArgs e)
        {
            StudentUpdate stdtUpdateWindow = new StudentUpdate();
            stdtUpdateWindow.Content = null;
            mainContent.Children.Add(stdtUpdateWindow.mainContent);
        }

        private void deleteStudentBtn_Click_1(object sender, RoutedEventArgs e)
        {
            StudentDelete stdtDeleteWindow = new StudentDelete();
            stdtDeleteWindow.Content = null;
            mainContent.Children.Add(stdtDeleteWindow.mainContent);
        }
        
        private void searchStudentBtn_Click_1(object sender, RoutedEventArgs e)
        {
            StudentSearch stdtSearchWindow = new StudentSearch();
            stdtSearchWindow.Content = null;
            mainContent.Children.Add(stdtSearchWindow.mainContent);
        }

        private void stdtCourseTitleCmbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Database.populateCourseID_Add(this);
        }

        private void stdtAdd_Click_1(object sender, RoutedEventArgs e)
        {
            Database.CreateStudent(this);

            Student student = new Student();
            student.StudentID = Convert.ToInt32(stdtIDAddTxt.Text);
            student.FirstName = stdtFNameTxt.Text;
            student.Surname = stdtSurnameTxt.Text;
            student.ALine1 = stdtALine1Txt.Text;
            student.ALine2 = stdtALine2Txt.Text;
            student.ACity = stdtCityTxt.Text;
            student.StudentACounty = stdtCountyTxt.Text;
            student.APostcode = stdtPostcodeTxt.Text;
            student.CourseID = courseID;
            student.TelNo = stdtTelNoTxt.Text;
            student.Email = stdtEmailTxt.Text;
        }

        private void restStdtAdd_Click_1(object sender, RoutedEventArgs e)
        {
            stdtFNameTxt.Text = "";
            stdtSurnameTxt.Text = "";
            stdtALine1Txt.Text = "";
            stdtALine2Txt.Text = "";
            stdtCityTxt.Text = "";
            stdtCountyTxt.Text = "";
            stdtPostcodeTxt.Text = "";
            stdtTelNoTxt.Text = "";
            stdtEmailTxt.Text = "";
            stdtPicTxt.Text = "";
            stdtCourseTitleCmbx.SelectedItem = "";
            stdtCourseIDLbl.Content = "Course ID";
            stdtPwrdAddTxt.Password = "";
            stdtPwrd1AddTxt.Password = "";

            stdtAddedLblGrid.Visibility = Visibility.Hidden;
            validStdtPasswordLbl.Visibility = Visibility.Hidden;
            matchStdtPasswordLbl.Visibility = Visibility.Hidden;
            stdtEmailValidationLbl.Visibility = Visibility.Hidden;
        }

        private void stdtPwrdAddTxt_LostFocus_1(object sender, RoutedEventArgs e)
        {
            Database.ValidatePassword_StdtAdd(this);
        }

        private void stdtPwrd1AddTxt_LostFocus_1(object sender, RoutedEventArgs e)
        {
            Database.ComparePasswords_stdtAdd(this);
        }

        private void stdtEmailTxt_LostFocus_1(object sender, RoutedEventArgs e)
        {
            Database.IsValidStdtEmail_Add(this);
        }

        private void stdtPostcodeTxt_LostFocus_1(object sender, RoutedEventArgs e)
        {
            Database.isValidPostcode(this);
        } 
    }
}
